<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <globalSearchTemplate :config="globalConfig" v-model="listQuery.userId" placeholder="请选择用户" prop="userId"
                            type="select" :load="userRemoteDataUrl"></globalSearchTemplate>
      <globalSearchTemplate :config="globalConfig" v-model="listQuery.orderStatus" placeholder="请选择订单状态"
                            prop="orderStatus"
                            type="select"></globalSearchTemplate>
      <globalSearchTemplate :config="globalConfig" v-model="listQuery.payMethod" placeholder="请选择支付方式" prop="payMethod"
                            type="select"></globalSearchTemplate>
      <globalSearchTemplate :config="globalConfig" v-model="listQuery.storeId" placeholder="请选择店铺" prop="storeId"
                            type="select" :load="storeRemoteDataUrl"></globalSearchTemplate>
      <globalSearchTemplate :config="globalConfig" v-model="listQuery.startTime" type="date"
                            placeholder="开始时间"></globalSearchTemplate>
      <globalSearchTemplate :config="globalConfig" v-model="listQuery.endTime" type="date"
                            placeholder="结束时间"></globalSearchTemplate>
      <el-button class="filter-item" v-waves @click="handleSearch" icon="el-icon-search">搜索</el-button>

    </div>
    <el-table :key='tableKey' :data="list" v-loading="listLoading" element-loading-text="给我一点时间" border fit
              highlight-current-row style="width: 100%">
      <globalTableColumn :data="list" :loading.sync="listLoading" :key="key" :config="globalConfig" align="center"
                         label="编号" prop="serialNumber" width="150"></globalTableColumn>
      <globalTableColumn :data="list" :loading.sync="listLoading" :key="key" :config="globalConfig" align="center"
                         type="select" label="订单用户" prop="userId" :load="userRemoteDataUrl"
                         width="120"></globalTableColumn>
      <globalTableColumn :data="list" :loading.sync="listLoading" :key="key" :config="globalConfig" align="center"
                         type="select" label="订单状态" prop="orderStatus" width="120">
      </globalTableColumn>
      <el-table-column label="收款信息" width="200px" align="center">
        <template slot-scope="scope">
          <div>
            <div>{{'收货姓名: ' + scope.row.consignee}}</div>
            <div>{{'收货电话: ' + scope.row.phone}}</div>
            <div>{{'收货地址: ' + scope.row.address}}</div>
            <div>{{'订单备注: ' + scope.row.memo}}</div>
          </div>
        </template>
      </el-table-column>
      <globalTableColumn :data="list" :loading.sync="listLoading" :config="globalConfig" align="center" type="select"
                         label="支付方式" prop="payMethod" width="120"></globalTableColumn>
      <globalTableColumn :data="list" :loading.sync="listLoading" :key="key" :config="globalConfig" align="center"
                         label="支付时间" prop="payDate" width="170"></globalTableColumn>

      <globalTableColumn :data="list" :loading.sync="listLoading" :key="key" :config="globalConfig" align="center"
                         label="总运费" prop="freightTotalPrice" width="100"></globalTableColumn>
      <globalTableColumn :data="list" :loading.sync="listLoading" :key="key" :config="globalConfig" align="center"
                         label="订单金额" prop="actualAmount" width="100"></globalTableColumn>

      <globalTableColumn :data="list" :loading.sync="listLoading" :key="key" :config="globalConfig" align="center"
                         label="创建时间" prop="createDate" width="170"></globalTableColumn>
      <el-table-column label="操作" width="260">
        <template slot-scope="scope">
          <el-button v-if="ordersManager_btn_upd" size="small" type="success" @click="handleUpdate(scope.row)">编辑
          </el-button>
          <el-button v-if="ordersManager_btn_find_sub" size="small" type="success"
                     @click="handleFindSubOrders(scope.row)">查看子订单
          </el-button>
          <el-button v-if="ordersManager_btn_del" size="mini" type="danger" @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div v-show="!listLoading" class="pagination-container">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                     :current-page.sync="listQuery.page"
                     :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit"
                     layout="total, sizes, prev, pager, next, jumper"
                     :total="total">
      </el-pagination>
    </div>
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" width="65%">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">

        <el-card class="box-card">
          <h3>订单信息</h3>
          <el-row type="flex" class="row-bg">
            <el-col :span="9">
              <globalFormItem :key="formKey" :config="globalConfig" label="订单用户" v-model="form.userId" prop="userId"
                              placeholder="请选择订单用户" type="select" :load="userRemoteDataUrl"></globalFormItem>
            </el-col>
            <el-col :span="9">
              <globalFormItem :key="formKey" :config="globalConfig" label="所属店铺" v-model="form.storeId" prop="storeId"
                              placeholder="请选择所属店铺" type="select" :load="storeRemoteDataUrl"></globalFormItem>
            </el-col>
          </el-row>

          <el-row type="flex" class="row-bg">
            <el-col :span="9">
              <globalFormItem :key="formKey" :config="globalConfig" label="订单状态" v-model="form.orderStatus"
                              prop="orderStatus"
                              placeholder="请选择订单状态" type="select"></globalFormItem>
            </el-col>
            <el-col :span="9">
              <globalFormItem :key="formKey" :config="globalConfig" label="订单号" v-model="form.serialNumber"
                              prop="serialNumber"
                              placeholder="请输入订单号"></globalFormItem>

            </el-col>
          </el-row>

          <el-row type="flex" class="row-bg">
            <el-col :span="9">
              <globalFormItem :key="formKey" :config="globalConfig" label="支付方式" v-model="form.payMethod"
                              prop="payMethod"
                              placeholder="请选择支付方式" type="select"></globalFormItem>
            </el-col>
            <el-col :span="9">
              <globalFormItem :key="formKey" :config="globalConfig" label="支付时间" v-model="form.payDate" prop="payDate"
                              placeholder="请输入支付时间" type="date"></globalFormItem>

            </el-col>
          </el-row>
        </el-card>

        <el-card class="box-card">
          <h3>收货信息</h3>
          <el-row type="flex" class="row-bg">
            <el-col :span="9">
              <globalFormItem :key="formKey" :config="globalConfig" label="收货人" v-model="form.consignee"
                              prop="consignee"
                              placeholder="请输入收货人" suffix-icon="el-icon-edit el-input__icon"></globalFormItem>
            </el-col>
            <el-col :span="9">
              <globalFormItem :key="formKey" :config="globalConfig" label="收货电话" v-model="form.phone" prop="phone"
                              placeholder="请输入收货电话" suffix-icon="el-icon-edit el-input__icon"></globalFormItem>
            </el-col>
          </el-row>

          <el-row type="flex" class="row-bg">
            <el-col :span="9">
              <el-form-item label="收货地址" prop="address" style="width: 420px">
                <el-input v-model="form.address" :rows="3" type="textarea" placeholder="请输入收货地址"/>
              </el-form-item>
            </el-col>
            <el-col :span="9">
              <el-form-item label="订单备注" prop="memo" style="width: 420px">
                <el-input v-model="form.memo" :rows="3" type="textarea" placeholder="请输入订单备注"/>
              </el-form-item>
            </el-col>
          </el-row>
        </el-card>


        <el-card class="box-card">
          <h3>金额信息</h3>
          <el-row type="flex" class="row-bg">
            <el-col :span="9">
              <globalFormItem :key="formKey" :config="globalConfig" label="现金总价格" v-model="form.retailTotalPrice"
                              prop="retailTotalPrice"
                              placeholder="请输入现金总价格" suffix-icon="el-icon-edit el-input__icon"></globalFormItem>
            </el-col>
            <el-col :span="9">
              <globalFormItem :key="formKey" :config="globalConfig" label="总运费" v-model="form.freightTotalPrice"
                              prop="freightTotalPrice"
                              placeholder="请输入总运费" suffix-icon="el-icon-edit el-input__icon"></globalFormItem>
            </el-col>
          </el-row>

          <el-row type="flex" class="row-bg">
            <el-col :span="9">
              <globalFormItem :key="formKey" :config="globalConfig" label="实付订单金额" v-model="form.actualAmount"
                              prop="actualAmount"
                              placeholder="请输入实付订单金额" suffix-icon="el-icon-edit el-input__icon"></globalFormItem>
            </el-col>
            <el-col :span="9">


            </el-col>
          </el-row>
        </el-card>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel('form')">取 消</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="create('form')">确 定</el-button>
        <el-button v-else type="primary" @click="update('form')">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style scoped>
  .filter-item {
    margin-left: 10px;
  }
  .el-card__body {
    padding: 10px !important;
  }

  .el-card {
    padding-top: 10px;
  }
</style>

<script>
  import {
    fetchList,
    getObj,
    addObj,
    putObj,
    delObj,
    casObj
  } from '@/api/order/orders'
  import { remoteData as orderUserInfoRemoteData } from '@/api/user/usersInfo'
  import { remoteData as storeInfoRemoteData } from '@/api/store/store.js'
  import { remoteData as shippingInfoRemoteData } from '@/api/order/shippingInfo.js'
  import waves from '@/directive/waves/index.js' // 水波纹指令
  import {
    mapGetters
  } from 'vuex'
  import {
    globalNotify
  } from '@/util/notifyUtils' //弹窗
  import {
    getToken
  } from '@/util/auth'
  import globalSearchTemplate from '@/components/global-el-search-template'
  import globalTableColumn from '@/components/global-el-table-column'
  import globalFormItem from '@/components/global-el-form-item'
  import { remoteData as userRemoteData } from '@/api/user/usersInfo'
  import { remoteData as storeRemoteData } from '@/api/store/store'

  export default {
    name: 'orders',
    components: {
      globalSearchTemplate,
      globalTableColumn,
      globalFormItem
    },
    directives: {
      waves
    },
    data() {
      return {
        userRemoteDataUrl: userRemoteData,
        storeRemoteDataUrl: storeRemoteData,
        globalConfig: {
          module: ['order'],
          componentName: ['orders']
        },
        list: null,
        total: null,
        listLoading: true,
        listQuery: {
          page: 1,
          limit: 20,
          'startTime': undefined,
          'endTime': undefined,
          'address': undefined,
          'consignee': undefined,
          'memo': undefined,
          'phone': undefined,
          'quantity': undefined,
          'shippingName': undefined,
          'shippingId': undefined,
          'orderStatus': undefined,
          'userId': undefined,
          'payMethod': undefined,
          'payDate': undefined,
          'storeId': undefined,
          'retailTotalPrice': undefined,
          'freightTotalPrice': undefined,
          'actualAmount': undefined,
          'thirdPartyPayId': undefined,
          'payStartDate': undefined,
          'payEndDate': undefined
        },
        rules: {},
        form: {
          'address': undefined,
          'consignee': undefined,
          'memo': undefined,
          'phone': undefined,
          'orderStatus': undefined,
          'userId': undefined,
          'payMethod': undefined,
          'payDate': undefined,
          'retailTotalPrice': undefined,
          'freightTotalPrice': undefined,
          'actualAmount': undefined,
          'thirdPartyPayId': undefined
        },
        dialogFormVisible: false,
        dialogStatus: '',
        ordersManager_btn_add: false,
        ordersManager_btn_upd: false,
        ordersManager_btn_del: false,
        ordersManager_btn_find_sub: false,
        textMap: {
          update: '编辑',
          create: '创建'
        },
        tableKey: 0,
        headers: {
          Authorization: 'Bearer ' + getToken()
        },
        casDataUrl: casObj,
        orderUserInfoRemoteDataUrl: orderUserInfoRemoteData,
        storeInfoRemoteDataUrl: storeInfoRemoteData,
        shipListRemoteDataUrl: shippingInfoRemoteData,
        key: '',
        formKey: ''
      }
    },
    computed: {
      ...mapGetters([
        'permissions'
      ])
    },
    filters: {},
    created() {
      this.getList()
      this.ordersManager_btn_add = this.permissions['ordersManager_btn_add']
      this.ordersManager_btn_upd = this.permissions['ordersManager_btn_upd']
      this.ordersManager_btn_del = this.permissions['ordersManager_btn_del']
      this.ordersManager_btn_find_sub = this.permissions['ordersManager_btn_find_sub']
    },
    methods: {
      getList() {
        this.listLoading = true
        fetchList(this.listQuery).then(response => {
          this.list = response.data.data.records
          this.refreshTable()
          this.total = response.data.data.total
          this.listLoading = false
        })
      },
      handleSizeChange(val) {
        this.listQuery.limit = val
        this.getList()
      },
      handleCurrentChange(val) {
        this.listQuery.page = val
        this.getList()
      },
      handleDelete(row) {
        delObj(row.id)
          .then(response => {
            this.dialogFormVisible = false
            this.getList()
            globalNotify(this, response)
          })
      },
      handleCreate() {
        this.dialogStatus = 'create'
        this.dialogFormVisible = true
        this.resetForm()
        this.refreshForm()
      },
      refreshTable() {
        this.key = new Date().getTime()
      },
      refreshForm() {
        this.formKey = new Date().getTime()
      },
      handleUpdate(row) {
        this.resetForm()
        getObj(row.id).then(response => {
          this.form = response.data.data
          this.refreshForm()
          this.dialogFormVisible = true
          this.dialogStatus = 'update'
        })
      },
      handleFindSubOrders(row) {
        this.$router.push({ path: '/order/orders/detail', query: { orderId: row.id } })
      },
      create(formName) {
        const set = this.$refs
        set[formName].validate(valid => {
          if (valid) {
            addObj(this.form)
              .then((response) => {
                this.dialogFormVisible = false
                this.getList()
                globalNotify(this, response)
              })
          } else {
            return false
          }
        })
      },
      cancel(formName) {
        this.resetForm()
        this.dialogFormVisible = false
      },
      update(formName) {
        const set = this.$refs
        set[formName].validate(valid => {
          if (valid) {
            this.dialogFormVisible = false
            this.form.password = undefined
            putObj(this.form).then((response) => {
              this.dialogFormVisible = false
              this.getList()
              globalNotify(this, response)
            })
          } else {
            return false
          }
        })
      },
      resetForm() {
        this.form = {
          'address': undefined,
          'consignee': undefined,
          'memo': undefined,
          'phone': undefined,
          'quantity': undefined,
          'shippingName': undefined,
          'shippingId': undefined,
          'orderStatus': undefined,
          'userId': undefined,
          'payMethod': undefined,
          'payDate': undefined,
          'storeId': undefined,
          'retailTotalPrice': undefined,
          'freightTotalPrice': undefined,
          'actualAmount': undefined,
          'thirdPartyPayId': undefined
        }
      },
      handleSearch() {
        this.listQuery.page = 1
        this.getList()
      }
    }
  }
</script>
